import type { App } from 'vue'
import { ElMessage, ElMessageBox, ElNotification, ElLoading } from 'element-plus'

// 全局属性类型
declare module '@vue/runtime-core' {
  interface ComponentCustomProperties {
    $message: typeof ElMessage
    $msgBox: typeof ElMessageBox
    $alert: typeof ElMessageBox.alert
    $confirm: typeof ElMessageBox.confirm
    $prompt: typeof ElMessageBox.prompt
    $notify: typeof ElNotification
    $loading: typeof ElLoading
  }
}

// 设置全局属性
export const setupGlobalProperties = (app: App) => {
  // 消息提示
  app.config.globalProperties.$message = ElMessage
  app.config.globalProperties.$msgBox = ElMessageBox
  app.config.globalProperties.$alert = ElMessageBox.alert
  app.config.globalProperties.$confirm = ElMessageBox.confirm
  app.config.globalProperties.$prompt = ElMessageBox.prompt
  app.config.globalProperties.$notify = ElNotification
  app.config.globalProperties.$loading = ElLoading

  // 全局方法
  app.config.globalProperties.$sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms))
  
  // 全局工具函数
  app.config.globalProperties.$formatDate = (date: string | Date) => {
    // 这里可以使用dayjs或其他日期库
    return new Date(date).toLocaleString()
  }
  
  app.config.globalProperties.$formatFileSize = (size: number) => {
    if (size < 1024) return `${size} B`
    if (size < 1024 * 1024) return `${(size / 1024).toFixed(2)} KB`
    if (size < 1024 * 1024 * 1024) return `${(size / (1024 * 1024)).toFixed(2)} MB`
    return `${(size / (1024 * 1024 * 1024)).toFixed(2)} GB`
  }
}
